package com.zxy.supplier_system.server.utils.wechat.official

import com.zxy.supplier_system.server.utils.wechat.validate200ErrorCodeAndMessageResponse
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.RequestEntity


data class BatchGetUserInfoRequest(
    val user_list: List<Item>
) {
    data class Item(
        val openid: String,
        val lang: String = "zh_CN"
    )
}

fun WechatOfficialAccountHelper.batchGetUserInfo(request: BatchGetUserInfoRequest): BatchGetUserInfoResponse {
    val accessToken = this.getOrFetchAccessToken()

    val requestEntity =
        RequestEntity.post("https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=$accessToken").body(request)
    val responseEntity = restTemplate.exchange(
        requestEntity,
        object : ParameterizedTypeReference<Map<String, Any>>() {},
    )
    return validate200ErrorCodeAndMessageResponse(responseEntity, BatchGetUserInfoResponse::class.java)
}

data class BatchGetUserInfoResponse(
    val user_info_list: List<User>
) {
    class User(
        val openid: String,
        val unionid: String?,
    )
}